﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using XDeal.BL.Implements;
using Utility;
using XDeal.Common.Web;
using XDeal.Common.Constants;
using XDeal.DAL.Entities;
using Utility.Enums;

namespace XDEALWEB.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DoLogIn()
        {
            string u = Request.QueryString["u"].ToString();
            string p = Request.QueryString["p"].ToString();
            p = PasswordManage.GeneratePasswordMD5(p);
            Customer cus = new Customer();
            CustomerBL cusBL = new CustomerBL();
            cus = cusBL.DoLogIn(u, p);

            if (cus != null)
            {
                //Update customer
                cus.LastVisit_dt = DateTime.Now;
                cus.VisitCount_i++;

                cusBL.Update(cus);

                if (GlobalVariables.Customer == null)
                {
                    GlobalVariables.Customer = cus;
                }

                if (GlobalVariables.OrderDeals == null)
                {
                    return Content("2");//Giỏ hang trống
                }

                return Content("1");
            }
            return Content("0");
        }
        [HttpPost]
        public ActionResult DoRegist(string fullName, string email, string pass, int province, int district, string Address, string Phone)
        {
            Province prov = new Province();
            ProvinceBL provBL = new ProvinceBL();
            prov = provBL.GetByID(province);

            District dist = new District();
            DistrictBL distBL = new DistrictBL();
            dist = distBL.GetByID(district);


            if (prov != null && dist != null && CheckEmailAvailableRegist(email))
            {
                Customer customer = new Customer();
                CustomerBL customerBL = new CustomerBL();

                customer.FullName_s = ConvertManage.ConvertToString(fullName, "");
                customer.Email_s = ConvertManage.ConvertToString(email, "");
                customer.Password_s = PasswordManage.GeneratePasswordMD5(pass);
                customer.DistrictID_s = dist.ID_i;
                customer.Address_s = ConvertManage.ConvertToString(Address, "");
                customer.Mobile_s = ConvertManage.ConvertToString(Phone, "");
                customer.DateAdded_dt = DateTime.Now;
                customer.LastVisit_dt = DateTime.Now;
                customer.VisitCount_i = 1;
                customer.CMoney_db = 0;
                customer.IsMailling_b = true;
                customer.Status_b = true;


                if (customerBL.Insert(customer))
                {
                    GlobalVariables.Customer = customer;
                    if (GlobalVariables.OrderDeals == null)
                    {
                        return Content("2");//Gio hàng trống
                    }
                    return Content("1");   
                }
                else
                {
                    return Content("0");   
                }
            }
            else
            {
                return Content("0");   
            }
        }


        /// <summary>
        /// Kiem tra dia chi email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        private bool CheckEmailAvailableRegist(string email)
        {
            CustomerBL cusBL = new CustomerBL();
            List<Customer> list = new List<Customer>();
            list = cusBL.GetCustomerByEmail(email);
            if (list.Count == 0)
            {
                return true;
            }
            return false;
        }
    }
}
